# -*- coding:utf-8 -*-
from Trajectories import *
from Volumes import *
from Weathers import *
from pre_processing import *
import datetime
import numpy as np

eps=0.001
def get(mode,h):
    p=-100
    if mode==0 and h in [5,6,7,8]:
        p=h-6
    if mode==1 and h in [7,8,9,10]:
        p=h-8
    if mode==2 and h in [14,15,16,17]:
        p=h-15
    if mode==3 and h in [16,17,18,19]:
        p=h-17
    return p

def get2(mode,h):
    p=-1
    if mode==0 and h in [6,7]:
        p=h-6
    if mode==1 and h in [8,9]:
        p=h-8
    if mode==2 and h in [15,16]:
        p=h-15
    if mode==3 and h in [17,18]:
        p=h-17
    return p

def get3(h):
    p=-1
    if h in [6,7]:
        p=0
    if h in [8,9]:
        p=1
    if h in [15,16]:
        p=2
    if h in [17,18]:
        p=3
    return p

'''
    6-8、8-10、15-17、17-19四个时间段
    6*6维代表6个20分钟时间段6条路径(A2 A3 B1 B3 C1 C3)平均时间
    6*5维代表6个20分钟时间段5个方向(10 11 20 30 31)平均车流量
    5*k个天气维度
    7个星期维度
    无2016-10-10、2016-09-01~10天气信息
    无2016-09-29的21时的天气信息
    去掉2016.9.15~17中秋(18日调休工作)，2016.10.1~7国庆(8、9调休工作)
'''
def buildTrajectories(trajectories,mode):
    tra=[]
    num=[]
    d=trajectories.available_dates()
    l=len(d)
    for i in range(0,l):
        line=[0.0]*36
        tra.append(line)
        line=[0.0]*36
        num.append(line)
    for i in trajectories.trajectories:
        t=i.starting_time
        p=get(mode,t.hour)
        p=p*3+t.minute/20
        o1=i.intersection_id
        o2=i.tollgate_id
        if o1=='A' and o2==2:
            q=0
        if o1=='A' and o2==3:
            q=1
        if o1=='B' and o2==1:
            q=2
        if o1=='B' and o2==3:
            q=3
        if o1=='C' and o2==1:
            q=4
        if o1=='C' and o2==3:
            q=5
        p=p*6+q
        tt=datetime.date(t.year,t.month,t.day)
        for j in range(0,l):
            if d[j]==tt:
                for k in range(-1,2):
                    pp=p+k*6
                    ss=0.1**abs(k)
                    if pp>=0 and pp<36:
                        tra[j][pp]+=i.travel_time*ss
                        num[j][pp]+=ss
                break
    for i in range(0,l):
        for j in range(0,36):
            if num[i][j]>0:
                tra[i][j]/=num[i][j]
    print 'tot day:',l
    return tra

def buildVolumes(volumes,mode):
    vol=[]
    d=volumes.available_dates()
    l=len(d)
    for i in d:
        line=[0.0]*90
        vol.append(line)
    s=0
    for i in volumes.volumes:
        t=i.rec_time
        p=get2(mode,t.hour)
        if p==-1:
            continue
        p=p*3+t.minute/20
        o1=i.tollgate_id
        o2=i.direction
        if o1==1 and o2==0:
            q=0
        if o1==1 and o2==1:
            q=1
        if o1==2 and o2==0:
            q=2
        if o1==3 and o2==0:
            q=3
        if o1==3 and o2==1:
            q=4
        p=p*5+q
        tt=datetime.date(t.year,t.month,t.day)
        for j in range(0,l):
            if d[j]==tt:
                s+=1
                vol[j][p]+=1
                if i.has_etc==0:
                    vol[j][p+30]+=1
                if i.vehicle_model<2:
                    vol[j][p+60]+=1
                break
    print s,len(volumes.volumes)
    print 'tot day:',l
    return vol

def check(rec):
    if rec.month==8 and rec.day>=29 and rec.day<=31:
        return False
    if rec.month==9 and rec.day>=1 and rec.day<=5:
        return False
    if rec.month==9 and rec.day>=15 and rec.day<=18:
        return False
    if rec.month==9 and rec.day==30:
        return False
    if rec.month==10 and rec.day>=1 and rec.day<=7:
        return False
    return True

def calTrajectories(trajectories):
    res=[]
    num=[]
    d=trajectories.available_dates()
    l=len(d)
    for i in range(0,6):
        res.append([])
        num.append([])
    for i in range(0,6):
        for j in d:
            line=[0.0]*4
            res[i].append(line)
            line=[0.0]*4
            num[i].append(line)
    for i in trajectories.trajectories:
        t=i.starting_time
        p=get3(t.hour)
        o1=i.intersection_id
        o2=i.tollgate_id
        if o1=='A' and o2==2:
            q=0
        if o1=='A' and o2==3:
            q=1
        if o1=='B' and o2==1:
            q=2
        if o1=='B' and o2==3:
            q=3
        if o1=='C' and o2==1:
            q=4
        if o1=='C' and o2==3:
            q=5
        tt=datetime.date(t.year,t.month,t.day)
        if check(tt):
            for j in range(0,l):
                if d[j]==tt:
                    res[q][j][p]+=i.travel_time
                    num[q][j][p]+=1
    for i in range(0,6):
        for j in range(0,len(d)):
            for k in range(0,4):
                if num[i][j][k]>0:
                    res[i][j][k]/=num[i][j][k]
    return res

def calVolumes(volumes):
    res=[]
    d=volumes.available_dates()
    l=len(d)
    for i in range(0,5):
        res.append([])
    for i in range(0,5):
        for j in d:
            line=[0.0]*4
            res[i].append(line)
    s=0
    for i in volumes.volumes:
        t=i.rec_time
        p=get3(t.hour)
        if p==-1:
            continue
        o1=i.tollgate_id
        o2=i.direction
        if o1==1 and o2==0:
            q=0
        if o1==1 and o2==1:
            q=1
        if o1==2 and o2==0:
            q=2
        if o1==3 and o2==0:
            q=3
        if o1==3 and o2==1:
            q=4
        tt=datetime.date(t.year,t.month,t.day)
        if check(tt):
            for j in range(0,l):
                if d[j]==tt:
                    res[q][j][p]+=1
                    break
    for i in range(0,5):
        for j in range(0,len(d)):
            for k in range(0,4):
                res[i][j][k]/=6
    return res

def buildWeathers(weathers):
    wea=[]
    days=[]
    line=[]
    s=0
    for i in weathers.weathers:
        if i.hour==0:
            line=[]
            s=0
        s+=1
        line.extend([i.pressure,i.wind_speed,i.temperature,i.rel_humidity,i.precipitation])
        if i.hour==21 and s==8 and check(i.rec_date):
            wea.append(line)
            days.append(i.rec_date)
        if i.hour==21:
            line=[]
            s=0
    return wea,days

def buildinput_task1(tra0,tra1,tra2,tra3,mode,days,wea,weadays):
    l1=len(days)
    l2=len(weadays)
    datas=[]
    for i in range(0,l1):
        if mode<2 and i==0:
            continue
        for j in range(0,l2):
            if days[i]==weadays[j]:
                line=[]
                if mode<2:
                    line.extend(tra0[i-1])
                    line.extend(tra1[i-1])
                    line.extend(tra0[i])
                if mode==1:
                    line.extend(tra1[i])
                if mode>1:
                    if i==0:
                        line.extend(tra0[-1])
                        line.extend(tra1[-1])
                    else:
                        line.extend(tra2[i-1])
                        line.extend(tra3[i-1])
                    line.extend(tra2[i])
                if mode==3:
                    line.extend(tra3[i])
                line.extend(wea[j])
                o=days[i].weekday()
                ll=[0]*7
                ll[o]=1
                line.extend(ll)
                datas.append(line)
    print 'input_task1 data number:',len(datas)
    return datas

def buildoutput_task1(tra,days,weadays):
    tra2=[]
    l=len(tra)
    for i in range(1,l):
        if days[i] in weadays:
            tra2.append(tra[i])
    labels=[]
    for i in range(0,6):
        m=[]
        for j in tra2:
            line=[]
            for k in range(i,36,6):
                line.append(j[k])
            m.append(line)
        labels.append(m)
    print 'output_task1 label number:',len(tra2)
    return labels

def buildinput_task2(vol,vol0,vol1,vol2,vol3,mode,days,wea,weadays):
    l1=len(days)
    l2=len(weadays)
    datas=[]
    for i in range(0,l1):
        if mode<2 and i<0:
            continue
        for j in range(0,l2):
            if days[i]==weadays[j]:
                line=[]
                if mode<2:
                #    line.extend(vol0[i-1])
                #    line.extend(vol1[i-1])
                    line.extend(vol0[i])
                #    line.extend(vol[i-7])
                if mode==1:
                    line.extend(vol1[i])
                if mode>1:
                #    if i==0:
                #        line.extend(vol0[-1])
                #        line.extend(vol1[-1])
                #    else:
                #        line.extend(vol2[i-1])
                #        line.extend(vol3[i-1])
                    line.extend(vol2[i])
                #    line.extend(vol[i-7])
                if mode==3:
                    line.extend(vol3[i])
                
                line.extend(wea[j])
                o=days[i].weekday()
                ll=[0]*7
                ll[o]=1
                line.extend(ll)
                datas.append(line)
    print 'input_task2 data number:',len(datas)
    return datas

def buildoutput_task2(vol,days,weadays):
    vol2=[]
    l=len(days)
    for i in range(0,l):
        if days[i] in weadays:
            vol2.append(vol[i])
    labels=[]
    for i in range(0,5):
        m=[]
        for j in vol2:
            line=[]
            for k in range(i,30,5):
                line.append(j[k])
            m.append(line)
        labels.append(m)
    print 'output_task2 label number:',len(vol2)
    return labels    
    
if __name__ == '__main__':
    trajectories=Trajectories()
#    volumes=Volumes()
    weathers=Weathers()
    trajectories2=Trajectories()
#    volumes2=Volumes()
    weathers2=Weathers()
    readTrajectories(trajectories)
#    readVolumes(volumes)
    readWeathers(weathers)
    readTrajectories2(trajectories2)
#    readVolumes2(volumes2)
    readWeathers2(weathers2)

    (wea,weadays)=buildWeathers(weathers)
    (wea2,weadays2)=buildWeathers(weathers2)

    res1=calTrajectories(trajectories)
    res2=calTrajectories(trajectories2)
    np.savez('./data_task1/average',trainRes=np.array(res1),testRes=np.array(res2))

    tra0=buildTrajectories(trajectories,0)
    tra1=buildTrajectories(trajectories,2)
    tra2=buildTrajectories(trajectories2,0)
    tra3=buildTrajectories(trajectories2,2)
    days=trajectories.available_dates()
    days2=trajectories2.available_dates()
    
    for i in range(0,2):
        datas=buildinput_task1(tra0,tra1,tra2,tra3,i,days,wea,weadays)
        tra=buildTrajectories(trajectories,i*2+1)
        labels=buildoutput_task1(tra,days,weadays)
        np.savez('./data_task1/traindata_part'+str(i),datas=np.array(datas),labels=np.array(labels))
    
    for i in range(0,2):
        tests=buildinput_task1(tra0,tra1,tra2,tra3,i+2,days2,wea2,weadays2)
        np.savez('./data_task1/testdata_part'+str(i),tests=np.array(tests))

'''    
    res1=calVolumes(volumes)
    res2=calVolumes(volumes2)
    np.savez('./data_task2/average',trainRes=np.array(res1),testRes=np.array(res2))

    vol0=buildVolumes(volumes,0)
    vol1=buildVolumes(volumes,2)
    vol2=buildVolumes(volumes2,0)
    vol3=buildVolumes(volumes2,2)
    days=volumes.available_dates()
    days2=volumes2.available_dates()
    
    for i in range(0,2):
        vol=buildVolumes(volumes,i*2+1)
        datas=buildinput_task2(vol,vol0,vol1,vol2,vol3,i,days,wea,weadays)
        labels=buildoutput_task2(vol,days,weadays)
        np.savez('./data_task2/traindata_part'+str(i),datas=np.array(datas),labels=np.array(labels))

    for i in range(0,2):
        vol=buildVolumes(volumes,i*2+1)
        tests=buildinput_task2(vol,vol0,vol1,vol2,vol3,i+2,days2,wea2,weadays2)
        np.savez('./data_task2/testdata_part'+str(i),tests=np.array(tests))
'''

